public class PrefixSum {
    public int pivotIndex(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n + 1];//前缀和数组
        for (int i = 1; i < n + 1; i++) {
            dp[i] = dp[i - 1] + nums[i - 1];
        }

        for (int i = 1; i < n + 1; i++) {
            if (dp[i - 1] == dp[n] - dp[i]) {
                return i - 1;
            }
        }
        return -1;
    }
}
